package Share;

import java.io.IOException;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;

public class SecureOutputStream extends OutputStream {

	private OutputStream stream;
	private MessageDigest digest;
	
	public SecureOutputStream(OutputStream stream)
	{
		try {
			digest = MessageDigest.getInstance("SHA-512");
		} catch (NoSuchAlgorithmException e) {
			
		}
		this.stream = stream;
	}
	
	public void SetCipher(Cipher cipher) throws IOException
	{
		this.stream = new CipherOutputStream(stream, cipher);
	}
	
	public void write(int b) throws IOException {
		digest.update((byte) b);
		stream.write(b);
	}
	
	
	public byte[] getHash()
	{
		return digest.digest();
	}
	
	public void startHash()
	{
		digest.reset();
	}
	
	public void writeHash() throws IOException
	{
		stream.write(digest.digest());
		flush();
	}
}
